package com.ruoyi.carShop.controller;

import com.ruoyi.carShop.service.LoadService;

import com.ruoyi.carShop.service.MerchantCarService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.system.api.domain.OrderDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@RestController
public class LoadController {
    @Autowired
    private LoadService loadService;
    @Autowired
    private MerchantCarService merchantCarService;


    @RequestMapping("getInfo")
    public AjaxResult getToken(){
        //1 获取request对象
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        int mId = loadService.getToken(token);
        AjaxResult info = merchantCarService.getInfo((long) mId);
        return info;
    }

    @RequestMapping("getOrder")
    public TableDataInfo getOrder(){
        OrderDetails orderDetails = new OrderDetails();
        //获取商家Id
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        int mId = loadService.getToken(token);
        orderDetails.setSeller(mId);
        //获取当前时间
        LocalDate localDate = LocalDate.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(Date.from(localDate.atStartOfDay().toInstant(ZoneOffset.UTC)));
        System.out.println(formattedDate);
        orderDetails.setOrderTime(formattedDate);
        TableDataInfo list = merchantCarService.list(orderDetails);
        return list;
    }

}
